var token = selectByKey("like_token");
//ajax模板
function jshtml(data, scriptdom,orderId) {
    var mainmax = document.getElementById('mainlist');
    var html = "";
    for (var k in data) {
        var htmlm = scriptdom.innerHTML;
        htmlm = htmlm.replace(/\{\{src\}\}/, data[k].good_img);
        htmlm = htmlm.replace(/\{\{num\}\}/, data[k].good_num);
        htmlm = htmlm.replace(/\{\{h3\}\}/g, data[k].good_name);
        htmlm = htmlm.replace(/\{\{price\}\}/, data[k].good_price);
        htmlm = htmlm.replace(/\{\{id\}\}/, orderId);
        htmlm = htmlm.replace(/\{\{good_id\}\}/, data[k].good_id);
        html += htmlm;
    }
    mainmax.innerHTML = html;
};
function reEnter () {
    document.querySelector('.infoMsg').style.display = 'block';
    setTimeout(function() {
        window.location.href = '../personal/enter.html';
    },1500);
}
function operate(ids){
    var payOrder = document.getElementById("pay");
    var cancel = document.getElementById("cancel");
    var markT = document.getElementsByClassName("markT")[0];
    var sureT = markT.querySelector("#sureT");
    var cancleT = markT.querySelector("#cancleT");
    var orderId = document.getElementsByClassName("ddd")[0].getAttribute("data-id");
    var lists = document.getElementsByClassName("ddd");
    console.log(ids)
    Tap(payOrder,function(){
        window.location.href = "payorder.html?id="+ids;
    });
    Tap(cancel,function(){
        markT.style.display = "block";
    });
    Tap(sureT,function(){
        $.ajax({
            url:pathName+"/api/v1/member/order/cancel/"+orderId,
            type:"GET",
            headers:{
                "Accept":"application/json",
                "Authorization":"Bearer "+token
            },
            success:function(data){
                if(data == 1){
                    window.location.href = "orders.html#waitPay";
                }
            },
            error:function(){
                if(!token){
                    reEnter();
                }
            }
        })
    });
    Tap(cancleT,function(){
        markT.style.display = "none";
    });
    for(var i = 0; i < lists.length; i++){
        Tap(lists[i],function(e,dom){
            var goodId = dom.getAttribute("data-good_id");
            window.location.href = "../detail.html?id="+goodId;
        });
    }
}
window.onload = function(){
    var deliverId = location.search.substr(4);
    var orderMoney = document.querySelector(".p1 i");
    var orderID = document.querySelector(".p2 i");
    var orderAddress = document.querySelector(".p3 i");
    var totalNum = document.getElementById("total_num");
    var yunfei = document.getElementById("yunfei");
    var total = document.getElementById("total");
    var needPay = document.getElementById("needPay");
    var couponPrice = document.getElementById("couponPrice");
    var packingFee = document.getElementById("packingFee");
    var mask = document.querySelector(".mask");
    $.ajax({
        url:pathName+"/api/v1/member/order/"+deliverId,
        type:"GET",
        headers:{
            "Accept":"application/json",
            "Authorization":"Bearer "+token
        },
        success:function(data){
            console.log(data);
            mask.style.display = "none";
            orderMoney.innerHTML = data.order.pay_total;
            orderID.innerHTML = data.order.order_id;
            orderAddress.innerHTML = data.order.address+"/"+data.order.name+"/"+data.order.phone;
            totalNum.innerHTML = data.order.countGoods;
            yunfei.innerHTML = data.order.trans_cost;
            packingFee.innerHTML = data.order.packingFee + data.order.shortFreight;
            if(data.order.coupon_price){
                couponPrice.innerHTML = "-"+data.order.coupon_price;
            }else{
                couponPrice.innerHTML = 0;
            }

            var couPrice = parseFloat(data.order.coupon_price) || 0;
            total.innerHTML = (parseFloat(data.order.pay_total) + couPrice).toFixed(2);
            needPay.innerHTML = data.order.pay_total
            jshtml(data.order.goodDetail,listMode,data.order.id);
            var list_id = data.order.order_id;
            operate(list_id);
            //时间
            var createTime = data.order.created_at.split(" ");
            var preTime = new Date(Date.parse(data.order.created_at.replace(/-/g, "/")));
            preTime = preTime.getTime();
            $(".messg>div:first-child p:nth-child(2)").text(createTime[0]);
            $(".messg>div:first-child p:nth-child(3)").text(createTime[1]);
            //计算失效时间
            var preTime = data.order.created_at.replace(/-/g, "/");
            var date = Date.parse(preTime);
            var pre = new Date(date);
            pre.setHours(pre.getHours()+2);
            var nowTime = new Date().getTime();
            var timeCha = pre.getTime() - nowTime;
            var hours = Math.floor(timeCha/1000/3600);
            var minutes = (timeCha/1000-3600)>0 ? (Math.floor((timeCha/1000-3600)/60)) : (Math.floor(timeCha/1000/60));
            $(".info-time i:first-of-type").text(hours);
            $(".info-time i:last-of-type").text(minutes);
            setInterval(function(){
                var nowTime = new Date().getTime();
                var timeCha = pre.getTime() - nowTime;
                hours = Math.floor(timeCha/1000/3600);
                minutes = (timeCha/1000-3600)>0 ? (Math.floor((timeCha/1000-3600)/60)) : (Math.floor(timeCha/1000/60));
                $(".info-time i:first-of-type").text(hours);
                $(".info-time i:last-of-type").text(minutes);
            },1000);
        },
        error:function(){
            if(!token){
                reEnter();
            }
        }
    })
}
